
class Logger {
    constructor(public name: string) { }

    private padZeros(value: number, length: number) {
        var paddy = value.toString();
        while (paddy.length < length) paddy = '0' + paddy;
        return paddy;
    }

    public getCurrentTimestamp(): string {
        var now = new Date();
        var timestamp = now.getFullYear() + '-' +
            this.padZeros(now.getMonth(), 2) + '-' + this.padZeros(now.getDay(), 2) + ' ' +
            this.padZeros(now.getHours(), 2) + ':' + this.padZeros(now.getMinutes(), 2) + ':' +
            this.padZeros(now.getSeconds(), 2) + '.' + this.padZeros(now.getMilliseconds(), 3);
        return timestamp;
    }

    public isLogEnabled(): boolean {
        return (typeof (console) != 'undefined') && (typeof (console.log) != 'undefined');
    }

    public isInfoEnabled(): boolean {
        return this.isLogEnabled() && (typeof (console.info) != 'undefined');
    }

    public isWarnEnabled(): boolean {
        return this.isLogEnabled() && (typeof (console.warn) != 'undefined');
    }

    public isErrorEnabled(): boolean {
        return this.isLogEnabled() && (typeof (console.error) != 'undefined');
    }

    private formatMessage(message: any): string {
        return (this.name + ' | ' + this.getCurrentTimestamp() + ' | ' + message);
    }

    public log(message: any): void {
        if (!this.isLogEnabled()) return;
        console.log(this.formatMessage(message));
    }

    public info(message: any): void {
        if (this.isInfoEnabled()) {
            console.info(this.formatMessage(message));
        } else {
            this.log(this.formatMessage('INFO: ' + message));
        }
    }

    public warn(message: any): void {
        if (this.isWarnEnabled()) {
            console.warn(this.formatMessage(message));
        } else {
            this.log(this.formatMessage('WARN: ' + message));
        }
    }

    public error(message: any): void {
        if (this.isErrorEnabled()) {
            console.info(this.formatMessage(message));
        } else {
            this.log(this.formatMessage('ERROR: ' + message));
        }
    }
}